declare const module: any

import { NestFactory } from '@nestjs/core'
import { AppModule } from './app.module'

// 接入验证
import { ValidationPipe } from '@nestjs/common'

import { magenta, cyan } from 'chalk'

// 接入swagger文档
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'

// 全局过滤器
// 异常过滤器
import { HttpExceptionFilter } from './core/filter/http-exception/http-exception.filter'
// 全局响应体拦截器
import { TransformInterceptor } from './core/interceptor/transform/transform.interceptor'

async function bootstrap() {
  const app = await NestFactory.create(AppModule)

  // 允许跨域
  app.enableCors()

  app.setGlobalPrefix('api')

  app.useGlobalFilters(new HttpExceptionFilter())

  app.useGlobalInterceptors(new TransformInterceptor())

  app.useGlobalPipes(new ValidationPipe())

  // swagger文档
  // @link https://www.nestjs.com.cn/openapi/introduction
  const config = new DocumentBuilder()
    .setTitle('管理系统')
    .setDescription('中后台接口文档')
    .setVersion('1.0')
    .build()
  const document = SwaggerModule.createDocument(app, config)

  SwaggerModule.setup('docs', app, document)

  await app.listen(3000)

  console.log(' ')
  console.log(magenta('服务启动: http://localhost:3000'))
  console.log(' ')
  console.log(cyan('接口文档地址：http://localhost:3000/docs'))

  if (module.hot) {
    module.hot.accept()
    module.hot.dispose(() => app.close())
  }
}
bootstrap()
